import axios from "axios"; //引入axios
import { handleRequest, handleResponse } from "../axiosRequest/handle"
import { decryptECB, encryptECB } from "../crypt/index"


//如果不写全局变量 会导致拦截器多次拦截
const install = axios.create();
//私钥
const privateKey = 'QHilvk7D3Phq766hQHilvk7D3Phq766h';

//设置环境
const mode = "development"
if (mode === 'development') {
    install.defaults.baseURL = 'http://172.26.2.18:3001';
} else if (mode === 'production') {
    install.defaults.baseURL = 'http://10.0.0.23:3001';
}

// 设置请求超时->60s
install.defaults.timeout = 60 * 1000;

//post请求头的设置
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
//json
// axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
//加密模式用该type
install.defaults.headers.post['Content-Type'] = 'text/plain;charset=UTF-8';

// 添加请求拦截器
install.interceptors.request.use(config => {
    // return handleRequest(config);
    const reqUrl = config.url;
    const body = config.data;
    //判断是否session接口
    if (reqUrl.indexOf('000_000_006') !== -1) {
        //加密请求体
        const str = JSON.stringify(body);
        const newBody = encryptECB(str, privateKey);
        config.data = newBody;
    } else {
        // const sessionId = getCache("222")
        // config.headers.sessionId = sessionId;
        const str = JSON.stringify(body);
        //获取解密内容
        // const realKey = getCache("111")
        // const newBody = encryptECB(str, realKey);
        // config.data = newBody;
    }

    return config;
}, error => {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
install.interceptors.response.use(response => {
    const responseURL = response.request.responseURL;
    if (responseURL.indexOf('000_000_006') !== -1) {
        //统一解密
        //获取响应体
        const data = response.data;
        //获取解密内容
        const str = decryptECB(data, privateKey);
        //转为json对象
        response.data = JSON.parse(str);
        //获取签名生成真的是密钥
        const publicKey = response.data.data.publicKey;
        //算法生成（这里只是简单截取）
        const signature1 = privateKey.substring(0, 15);
        const signature2 = publicKey.substring(15, 32);
        const realKey = signature1 + signature2;
        const sessionId = response.data.data.sessionId;
        //持久化key和session
        // setCache("111", realKey)
        // setCache("222", sessionId)
    } else {
        //统一解密
        //获取响应体
        const data = response.data;
        //获取解密内容
        // const realKey = getCache("111")
        // const str = decryptECB(data, realKey);
        //转为json对象
        // response.data = JSON.parse(str);
    }
    return response;
}, error => {
    // 超出 2xx 范围的状态码都会触发该函数。
    // 对响应错误做点什么
    return Promise.reject(error);
});

/**
 * get方法，对应get请求
 * @param url
 * @param params
 */
export function get(url, params) {
    return install.get(url, {
        params: params
    });
}

/**
 * post方法，对应post请求
 * @param url
 * @param params
 */
export function post(url, params) {
    return install.post(url, params);
}